package com.jic.order.controller.score;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.RestResult;
import com.jic.order.api.ScoreOrderApi;
import com.jic.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@Slf4j
@Api(tags = {"Feign-积分"})
@RequestMapping(value = "scoreOrder")
@RestController
public class ScoreOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ScoreOrderApi scoreOrderApi;

    @ApiOperation(value = "查询邮政积分")
    @PostMapping("/queryUser")
    RestResult<JSONObject> queryUser(@RequestBody JSONObject param){
        RestResult<JSONObject> ret = scoreOrderApi.queryUser(param);
        return ret;
    }

    @ApiOperation(value = "支付积分")
    @PostMapping("/scorePay")
    RestResult<JSONObject> scorePay(@RequestBody JSONObject param){
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        param.put("orderNo",uuid);
        RestResult<JSONObject> ret = scoreOrderApi.scorePay(param);
        JSONObject scoreOrder = ret.getData();
        scoreOrder.put("order_no",uuid);
        scoreOrder.put("user_id",param.getString("userId"));
        scoreOrder.put("unionid",param.getString("unionid"));
        scoreOrder.put("status",0);
        orderService.insertScoreRecord(scoreOrder);
        return ret;
    }

    /**
     * 积分兑换查询
     * @param ret
     * @return
     */
    @PostMapping("/scorePayQuery")
    public RestResult<JSONObject> scorePayQuery(@RequestBody JSONObject ret){
        RestResult<JSONObject> re = scoreOrderApi.scorePayQuery(ret);
        ret = re.getData();
        String status = ret.getString("transStatus");
        Integer scStatus = 0;
        switch (status){
            case "00" : scStatus = 0;break;
            case "01" : scStatus = 1;break;
            case "02" : scStatus = 2;break;
        }
        ret.put("status",scStatus);
        ret.put("order_no",ret.getString("orderNo"));
        orderService.updateScoreRecord(ret);
        return re;
    }
}
